package net.lgpage.http;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ProgressBar;

public abstract class ProgressDialogTask<Result> extends ExceptionAsyncTask<Result> {

	public ProgressDialogTask(Context context, HttpExceptionDealer dealer) {
		super(context, dealer);
	}

	public ProgressDialogTask(Context context) {
		super(context);
	}

	Dialog dialog;

	ProgressBar progressBar;
	public static int CANCEL = 2;
	public static int OPEN = 1;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				openDialog();
				break;
			case 2:
				cancelDialog();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	public void setDialog(Dialog dialog) {
		this.dialog = dialog;
	}

	private void openDialog() {
		if (dialog == null)
			return;
		dialog.show();
	}

	private void cancelDialog() {
		if (dialog == null)
			return;
		dialog.cancel();
	}

	@Override
	public void onCancelled() {
		super.onCancelled();
	}

	@Override
	public void onPostExecute(Result result) {
		super.onPostExecute(result);
		handler.sendEmptyMessage(CANCEL);
	}

	@Override
	public void onException(Exception e) {
		super.onException(e);
		handler.sendEmptyMessage(CANCEL);
	}

	@Override
	public void onPreExecute() {
		handler.sendEmptyMessage(OPEN);
		super.onPreExecute();
	}

	
}
